package com.ckerchina.admin.ckadmin.utils;

import java.io.IOException;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;

import com.ckerchina.admin.ckadmin.utils.FluentUTF8ResponseHandlerWithState.ResponseHandlerResult;

public class FluentUTF8ResponseHandlerWithState implements ResponseHandler<ResponseHandlerResult> {

	@Override
	public ResponseHandlerResult handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
		int statusCode = response.getStatusLine().getStatusCode();
		String entity = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
		return new ResponseHandlerResult(statusCode, entity);
	}

	public static class ResponseHandlerResult {
		private int statusCode;

		private String entity;

		public ResponseHandlerResult(int statusCode, String entity) {
			super();
			this.statusCode = statusCode;
			this.entity = entity;
		}

		public int getStatusCode() {
			return statusCode;
		}

		public void setStatusCode(int statusCode) {
			this.statusCode = statusCode;
		}

		public String getEntity() {
			return entity;
		}

		public void setEntity(String entity) {
			this.entity = entity;
		}

		public String toString() {
			return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
		}

	}
}
